{% extends 'base.html' %}
{% load static %}

{% block title %}盘点计划管理{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0"><i class="fas fa-clipboard-list me-2"></i>盘点计划管理</h5>
                    <a href="{% url 'assets:inventory_plan_create' %}" class="btn btn-primary">
                        <i class="fas fa-plus me-1"></i>创建盘点计划
                    </a>
                </div>
                
                <div class="card-body">
                    <!-- 搜索和筛选 -->
                    <form method="get" class="row g-3 mb-4">
                        <div class="col-md-4">
                            <input type="text" class="form-control" name="search" 
                                   placeholder="搜索计划名称或描述..." value="{{ search_query }}">
                        </div>
                        <div class="col-md-3">
                            <select class="form-select" name="status">
                                <option value="">全部状态</option>
                                {% for value, label in status_choices %}
                                    <option value="{{ value }}" {% if value == selected_status %}selected{% endif %}>
                                        {{ label }}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-3">
                            <select class="form-select" name="plan_type">
                                <option value="">全部类型</option>
                                {% for value, label in type_choices %}
                                    <option value="{{ value }}" {% if value == selected_type %}selected{% endif %}>
                                        {{ label }}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-2">
                            <button type="submit" class="btn btn-outline-primary w-100">
                                <i class="fas fa-search me-1"></i>搜索
                            </button>
                        </div>
                    </form>

                    <!-- 盘点计划列表 -->
                    {% if page_obj %}
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>计划名称</th>
                                        <th>类型</th>
                                        <th>状态</th>
                                        <th>创建人</th>
                                        <th>计划时间</th>
                                        <th>进度</th>
                                        <th>资产数量</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for plan in page_obj %}
                                        <tr>
                                            <td>
                                                <strong>{{ plan.name }}</strong>
                                                {% if plan.description %}
                                                    <br><small class="text-muted">{{ plan.description|truncatechars:50 }}</small>
                                                {% endif %}
                                            </td>
                                            <td>
                                                <span class="badge bg-info">{{ plan.get_plan_type_display }}</span>
                                            </td>
                                            <td>
                                                {% if plan.status == 'draft' %}
                                                    <span class="badge bg-secondary">{{ plan.get_status_display }}</span>
                                                {% elif plan.status == 'active' %}
                                                    <span class="badge bg-primary">{{ plan.get_status_display }}</span>
                                                {% elif plan.status == 'completed' %}
                                                    <span class="badge bg-success">{{ plan.get_status_display }}</span>
                                                {% else %}
                                                    <span class="badge bg-danger">{{ plan.get_status_display }}</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                {% if plan.creator %}
                                                    {{ plan.creator.get_full_name|default:plan.creator.username }}
                                                {% else %}
                                                    <span class="text-muted">未知</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                <small>
                                                    计划：{{ plan.planned_start_date|date:"Y-m-d" }} ~ {{ plan.planned_end_date|date:"Y-m-d" }}<br>
                                                    {% if plan.actual_start_date %}
                                                        实际：{{ plan.actual_start_date|date:"Y-m-d" }}
                                                        {% if plan.actual_end_date %} ~ {{ plan.actual_end_date|date:"Y-m-d" }}{% endif %}
                                                    {% endif %}
                                                </small>
                                            </td>
                                            <td>
                                                {% if plan.status == 'active' %}
                                                    <div class="progress" style="height: 20px;">
                                                        <div class="progress-bar" role="progressbar" 
                                                             style="width: {{ plan.progress_percentage }}%"
                                                             aria-valuenow="{{ plan.progress_percentage }}" 
                                                             aria-valuemin="0" aria-valuemax="100">
                                                            {{ plan.progress_percentage }}%
                                                        </div>
                                                    </div>
                                                {% else %}
                                                    <span class="text-muted">-</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                <span class="badge bg-light text-dark">{{ plan.total_assets_count }}</span>
                                                {% if plan.discrepancy_count > 0 %}
                                                    <br><span class="badge bg-warning">{{ plan.discrepancy_count }} 差异</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                <div class="btn-group btn-group-sm" role="group">
                                                    <a href="{% url 'assets:inventory_plan_detail' plan.pk %}" 
                                                       class="btn btn-outline-primary" title="查看详情">
                                                        <i class="fas fa-eye"></i>
                                                    </a>
                                                    {% if plan.status == 'draft' %}
                                                        <a href="{% url 'assets:inventory_plan_start' plan.pk %}" 
                                                           class="btn btn-outline-success" title="启动盘点">
                                                            <i class="fas fa-play"></i>
                                                        </a>
                                                    {% endif %}
                                                </div>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>

                        <!-- 分页 -->
                        {% if page_obj.has_other_pages %}
                            <nav aria-label="盘点计划分页">
                                <ul class="pagination justify-content-center">
                                    {% if page_obj.has_previous %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page=1{% if search_query %}&search={{ search_query }}{% endif %}{% if selected_status %}&status={{ selected_status }}{% endif %}{% if selected_type %}&plan_type={{ selected_type }}{% endif %}">首页</a>
                                        </li>
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if selected_status %}&status={{ selected_status }}{% endif %}{% if selected_type %}&plan_type={{ selected_type }}{% endif %}">上一页</a>
                                        </li>
                                    {% endif %}

                                    <li class="page-item active">
                                        <span class="page-link">第 {{ page_obj.number }} 页，共 {{ page_obj.paginator.num_pages }} 页</span>
                                    </li>

                                    {% if page_obj.has_next %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if selected_status %}&status={{ selected_status }}{% endif %}{% if selected_type %}&plan_type={{ selected_type }}{% endif %}">下一页</a>
                                        </li>
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if search_query %}&search={{ search_query }}{% endif %}{% if selected_status %}&status={{ selected_status }}{% endif %}{% if selected_type %}&plan_type={{ selected_type }}{% endif %}">末页</a>
                                        </li>
                                    {% endif %}
                                </ul>
                            </nav>
                        {% endif %}
                    {% else %}
                        <div class="text-center py-5">
                            <i class="fas fa-clipboard-list fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">暂无盘点计划</h5>
                            <p class="text-muted">点击上方按钮创建第一个盘点计划</p>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
